<?php


namespace app\miniapp\common;


use app\common\cachemodel\NatalGoods;
use app\common\lib\ConfigManage;
use app\common\model\Recode;

class NatalReport
{
    /**
     * 获取本命报告商品
     * @param int $uid 用户ID
     * @param int $rid 档案ID
     * @param int $type 7 完整版本命报告 13 精简版本命报告
     * @param int $gid 排除的报告商品ID
     * @param null $unlockReport 已解锁的报告信息
     * @return array
     * @throws \Exception
     */
    public static function getGoods(int $uid, int $rid, int $type, int $gid = 0, $unlockReport = null): array
    {
        $now_time = time();
        $NatalReportModel = new \app\common\cachemodel\NatalReport();

        if (is_null($unlockReport))
        {
            if ($uid > 0)
            {
                $recodeInfo = (new Recode())->getRecode(['id' => $rid]);
                $unlockReport = $NatalReportModel->getNatalReport2($recodeInfo);
                // $unlockReport = $NatalReportModel->getNatalReport($uid, $rid);
            } else
            {
                $unlockReport = [];
            }
        }

        $goodData = (new NatalGoods())->getGoods($type);

        $gData = [];
        foreach ($goodData as $gitem)
        {
            $ks = (string)($gitem['is_recommend_index'] * 100000000 + $gitem['sort'] + ('0.' . $gitem['id']));
            $gData[$ks] = $gitem;
        }
        krsort($gData);

        $goodBtnCollor = [
            'feelings' => [ // 恋爱
                'btn_color' => '#FF6C89',
                'num_color' => '#FF8FAC',
                'num_bg_color' => '#FFC6D3',
            ],
            'dating' => [ // 婚姻
                'btn_color' => '#32C4FE',
                'num_color' => '#32C3FD',
                'num_bg_color' => '#ABE0FA',
            ],
            'carrer' => [ // 事业
                'btn_color' => '#6546F5',
                'num_color' => '#6546F4',
                'num_bg_color' => '#C8C2F8',
            ],
            'money' => [ // 财富
                'btn_color' => '#FFBA07',
                'num_color' => '#FEB907',
                'num_bg_color' => '#FBE19F',
            ],
            'academic' => [ // 学业
                'btn_color' => '#256AFD',
                'num_color' => '#4B93FF',
                'num_bg_color' => '#CEE0FC',
            ]
        ];

        $goodList = [];
        $goodRecommend = [];
        $orderModel = new \app\common\cachemodel\Orders();
        if ($type === 13)
        {   // 抖音小程序获取精简版的商品图片
            $natalReportImg = ConfigManage::simNatalReportGoodImage();
        } else
        {   //  完整版解锁图片
            $natalReportImg = ConfigManage::natalReportGoodImage(2);
        }

        foreach (array_values($gData) as $key => $item)
        {
            $unlock_num = 0;
            if ($item['status'] != 2)
            {
                $unlock_num = $item['base_num'] + $orderModel->getTotalPrepaidOrder($type, $item['id']);
            }

            if ($item['status'] == 2 || $gid == $item['id'])
            {   // 如果是首页 并且 没有推荐到首页
                continue;
            } else//判断商品解锁状态
            {
                if (isset($unlockReport[$item['id']]))
                {
                    $unlock_status = (int)$unlockReport[$item['id']]['status'] === 2 ? 2 : 1;
                    $status = isset($unlockReport[$item['id']]) ? ($unlock_status) : 0;
                } else
                {
                    $status = (int)($item['status'] == 1 ? 0 : 3);
                }
            }

            // TODO 学业报告临时活动
            if ((int)$item['id'] === 52 && $now_time >= 1595498400 && $now_time <= 1599321599)
                // if ((int)$item['id'] === 52 && $now_time >= 1595433600 && $now_time <= 1599321599) // 测试代码
            {
                $bgImg = $natalReportImg['large_active'][$item['flag']];
            } else
            {
                // todo 暂时默认全部长图
                $bgImg = $natalReportImg['large'][$item['flag']];
            }

            $gData = [
                'status' => $status,
                'bg_img' => $bgImg,
                'unlock_num' => $status === 3 ? 0 : $unlock_num,
                'type' => 0,
                'btn_color' => $goodBtnCollor[$item['flag']]['btn_color'] ?? '',
                'num_color' => $goodBtnCollor[$item['flag']]['num_color'] ?? '',
                'num_bg_color' => $goodBtnCollor[$item['flag']]['num_bg_color'] ?? '',
                'gid' => (int)($item['id'] ?? 0)
            ];

            if ($item['is_recommend_index'])
            {
                $goodRecommend[] = $gData;
            } else
            {
                $goodList[] = $gData;
            }
        }

        $goodList = array_merge($goodRecommend, $goodList);

        $goodContent = [];
        if ($goodList)
        {
            if ($type === 13)
            {
                // 深度预测页面
                $goodContent[] = [
                    'title' => '定制预测报告',
                    // 'desc' => '一次购买、终生免费升级',
                    'desc' => '',
                    'goods_list' => $goodList
                ];
            } else
            {
                $goodContent[] = [
                    'title' => '深度预测报告',
                    // 'desc' => '点击可查看得分意义',
                    'desc' => '',
                    'goods_list' => $goodList
                ];
            }
        }

        return $goodContent;
    }
}
